home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / test / test0.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  7KB  |  261 lines

  1. /* test 0 */
  2.  
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <stdio.h>
  8.  
  9. #define MAX_ERROR 4
  10.  
  11. extern int errno;
  12. int errct;
  13. int subtest;
  14. extern off_t lseek();
  15.  
  16.  
  17. #define NB          30L
  18. main()
  19. {
  20.   int i;
  21.   char let;
  22.  
  23.   printf("Test  0 ");
  24.   fflush(stdout);
  25.   for (i = 0; i < 4; i++) {
  26.     test00();
  27.     test01();
  28.   }
  29.   if (errct == 0)
  30.     printf("ok\n");
  31.   else
  32.     printf(" %d errors\n", errct);
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. char aa[100];
  41. char b[4] = {0, 1, 2, 3}, c[4] = {10, 20, 30, 40}, d[4] = {6, 7, 8, 9};
  42. long t1;
  43.  
  44. test00()
  45. {
  46.   /* Test program for open(), close(), creat(), read(), write(), lseek(). */
  47.   int i, n, n1, n2;
  48.   long t, time(), abuf[4];
  49.   struct stat s;
  50.  
  51.  
  52.   subtest = 1;
  53.   if ((n = creat("foop", 0777)) != 3) {
  54.     printf("Creating a file returned file descriptor %d instead of 3\n", n);
  55.     e(1);
  56.   }
  57.   if ((n1 = creat("foop", 0777)) != 4) e(2);
  58.   if ((n2 = creat("/", 0777)) != -1) e(3);
  59.   if (close(n) != 0) e(4);
  60.   if ((n = open("foop", O_RDONLY)) != 3) e(5);
  61.   if ((n2 = open("nofile", O_RDONLY)) != -1) e(6);
  62.   if (close(n1) != 0) e(7);
  63.  
  64.   /* N is the only one open now. */
  65.   for (i = 0; i < 2; i++) {
  66.     n1 = creat("File2", 0777);
  67.     if (n1 != 4) {
  68.         printf("creat yielded fd=%d, expected 4\n", n1);
  69.         e(8);
  70.     }
  71.     if ((n2 = open("File2", O_RDONLY)) != 5) e(9);
  72.     if (close(n1) != 0) e(10);
  73.     if (close(n2) != 0) e(11);
  74.   }
  75.   unlink("File2");
  76.   if (close(n) != 0) e(12);
  77.  
  78.   /* All files closed now. */
  79.   for (i = 0; i < 2; i++) {
  80.     t = 105L;
  81.     if ((n = creat("foop", 0777)) != 3) e(13);
  82.     if (close(n) != 0) e(14);
  83.     if ((n = open("foop", O_RDWR)) != 3) e(15);
  84.  
  85.     /* Read/write tests */
  86.     if (write(n, b, 4) != 4) e(16);
  87.     if (read(n, aa, 4) != 0) e(17);
  88.     if (lseek(n, 0L, SEEK_SET) != 0L) e(18);
  89.     if (read(n, aa, 4) != 4) e(19);
  90.     if (aa[0] != 0 || aa[1] != 1 || aa[2] != 2 || aa[3] != 3) e(20);
  91.     if (lseek(n, 0L, SEEK_SET) != 0L) e(21);
  92.     if (lseek(n, 2L, SEEK_CUR) != 2L) e(22);
  93.     if (read(n, aa, 4) != 2) e(23);
  94.     if (aa[0] != 2 || aa[1] != 3 || aa[2] != 2 || aa[3] != 3) e(24);
  95.     if (lseek(n, 2L, SEEK_SET) != 2L) e(25);
  96.     clraa();
  97.     if (write(n, c, 4) != 4) e(26);
  98.     if (lseek(n, 0L, SEEK_SET) != 0L) e(27);
  99.     if (read(n, aa, 10) != 6) e(28);
  100.     if (aa[0] != 0 || aa[1] != 1 || aa[2] != 10 || aa[3] != 20) e(29);
  101.     if (lseek(n, 16L, SEEK_SET) != 16L) e(30);
  102.     if (lseek(n, 2040L, SEEK_END) != 2046L) e(31);
  103.     if (read(n, aa, 10) != 0) e(32);
  104.     if (lseek(n, 0L, SEEK_CUR) != 2046L) e(33);
  105.     clraa();
  106.     if (write(n, c, 4) != 4) e(34);
  107.     if (lseek(n, 0L, SEEK_CUR) != 2050L) e(35);
  108.     if (lseek(n, 2040L, SEEK_SET) != 2040L) e(36);
  109.     clraa();
  110.     if (read(n, aa, 20) != 10) e(37);
  111.     if (aa[0] != 0 || aa[5] != 0 || aa[6] != 10 || aa[9] != 40) e(38);
  112.     if (lseek(n, 10239L, SEEK_SET) != 10239L) e(39);
  113.     if (write(n, d, 2) != 2) e(40);
  114.     if (lseek(n, -2L, SEEK_END) != 10239L) e(41);
  115.     if (read(n, aa, 2) != 2) e(42);
  116.     if (aa[0] != 6 || aa[1] != 7) e(43);
  117.     if (lseek(n, NB * 1024L - 2L, SEEK_SET) != NB * 1024L - 2L) e(44);
  118.     if (write(n, b, 4) != 4) e(45);
  119.     if (lseek(n, 0L, SEEK_SET) != 0L) e(46);
  120.     if (lseek(n, -6L, SEEK_END) != 1024L * NB - 4) e(47);
  121.     clraa();
  122.     if (read(n, aa, 100) != 6) e(48);
  123.     if (aa[0] != 0 || aa[1] != 0 || aa[3] != 1 || aa[4] != 2|| aa[5] != 3)
  124.         e(49);
  125.     if (lseek(n, 20000L, SEEK_SET) != 20000L) e(50);
  126.     if (write(n, c, 4) != 4) e(51);
  127.     if (lseek(n, -4L, SEEK_CUR) != 20000L) e(52);
  128.     if (read(n, aa, 4) != 4) e(53);
  129.     if (aa[0] != 10 || aa[1] != 20 || aa[2] != 30 || aa[3] != 40) e(54);
  130.     if (close(n) != 0) e(55);
  131.     if ((n1 = creat("foop", 0777)) != 3) e(56);
  132.     if (close(n1) != 0) e(57);
  133.     unlink("foop");
  134.  
  135.     /* Test clock. */
  136. /*  This test messes up the clock value.  Comment it out:
  137.   t = 20L;
  138.   stime(&t);
  139.   t = 0L;
  140.   t = time(&t);
  141.   if (t < 20L || t > 70L) e(59);
  142.   t = 200L;
  143.   if (stime(&t) < 0) e(60);
  144.   t = 0L;
  145.   if (time(&t) < 200L || time(&t) > 205L) e(61);
  146.   n1 = creat("timefile", 0777);
  147.   abuf[0] = 03003L;
  148.   abuf[1] = 03003L;
  149.   if (utime("timefile", abuf) < 0) e(62);
  150.   if (stat("timefile", &s) < 0) e(63);
  151.   if (s.st_atime != 03003) e(64);
  152.   abuf[0] = 06226;
  153.   abuf[1] = 06226;
  154.   if (utime("timefile", abuf) < 0) e(65);
  155.   if (stat("timefile", &s) < 0) e(66);
  156.   if (s.st_ctime != 06226) e(67);
  157.   if (unlink("timefile") < 0) e(68);
  158.   if (close(n1) < 0) e(69);
  159.   if (times(abuf) < 0) e(70);
  160.  
  161.   End of deleted code */
  162.   }
  163. }
  164.  
  165.  
  166. static clraa()
  167. {
  168.   int i;
  169.   for (i = 0; i < 100; i++) aa[i] = 0;
  170. }
  171.  
  172.  
  173. test01()
  174. {
  175. /* Test link, unlink, stat, fstat, dup, umask.
  176.  */
  177.  
  178.   int i, j, n, n1, flag;
  179.   char a[255], b[255];
  180.   struct stat s, s1;
  181.  
  182.   subtest = 2;
  183.   for (i = 0; i < 2; i++) {
  184.     umask(0);
  185.  
  186.     if ((n = creat("T3", 0702)) < 0) e(1);
  187.     if (link("T3", "newT3") < 0) e(2);
  188.     if ((n1 = open("newT3", O_RDWR)) < 0) e(3);
  189.     for (j = 0; j < 255; j++) a[j] = j;
  190.     if (write(n, a, 255) != 255) e(4);
  191.     if (read(n1, b, 255) != 255) e(5);
  192.     flag = 0;
  193.     for (j = 0; j < 255; j++)
  194.         if (a[j] != b[j]) flag++;
  195.     if (flag) e(6);
  196.     if (unlink("T3") < 0) e(7);
  197.     if (close(n) < 0) e(8);
  198.     if (close(n1) < 0) e(9);
  199.     if ((n1 = open("newT3", O_RDONLY)) < 0) e(10);
  200.     if (read(n1, b, 255) != 255) e(11);
  201.     flag = 0;
  202.     for (j = 0; j < 255; j++)
  203.         if (a[j] != b[j]) flag++;
  204.     if (flag) e(12);
  205.  
  206.     /* Now check out stat, fstat. */
  207.     if (stat("newT3", &s) < 0) e(13);
  208.     if (s.st_mode != 0100702) e(14);
  209.     if (s.st_nlink != 1) e(15);
  210.     if (s.st_size != 255L) e(16);
  211.     if (fstat(n1, &s1) < 0) e(17);
  212.     if (s.st_dev != s1.st_dev) e(18);
  213.     if (s.st_ino != s1.st_ino) e(19);
  214.     if (s.st_mode != s1.st_mode) e(20);
  215.     if (s.st_nlink != s1.st_nlink) e(21);
  216.     if (s.st_uid != s1.st_uid) e(22);
  217.     if (s.st_gid != s1.st_gid) e(23);
  218.     if (s.st_rdev != s1.st_rdev) e(24);
  219.     if (s.st_size != s1.st_size) e(25);
  220.     if (s.st_atime != s1.st_atime) e(26);
  221.     if (close(n1) < 0) e(27);
  222.     if (unlink("newT3") < 0) e(28);
  223.  
  224.     umask(040);
  225.     if ((n = creat("T3a", 0777)) < 0) e(29);
  226.     if (stat("T3a", &s) < 0) e(30);
  227.     if (s.st_mode != 0100737) e(31);
  228.     if (unlink("T3a") < 0) e(32);
  229.     if (close(n1) < 0) e(33);
  230.  
  231.     /* Dup */
  232.     if ((n = creat("T3b", 0777)) < 0) e(34);
  233.     if (close(n) < 0) e(35);
  234.     if ((n = open("T3b", O_RDWR)) < 0) e(36);
  235.     if ((n1 = dup(n)) != n + 1) e(37);
  236.     if (write(n, a, 255) != 255) e(38);
  237.     read(n1, b, 20);
  238.     if (lseek(n, 0L, SEEK_SET) != 0L) e(39);
  239.     if ((j = read(n1, b, 1024)) != 255) e(40);
  240.     if (unlink("T3b") < 0) e(41);
  241.     if (close(n) < 0) e(42);
  242.     if (close(n1) < 0) e(43);
  243.  
  244.   }
  245. }
  246.  
  247.  
  248. e(n)
  249. int n;
  250. {
  251.   int err_num = errno;        /* save errno in case printf clobbers it */
  252.  
  253.   printf("Subtest %d,  error %d  errno=%d  ", subtest, n, errno);
  254.   errno = err_num;        /* restore errno, just in case */
  255.   perror("");
  256.   if (errct++ > MAX_ERROR) {
  257.     printf("Too many errors; test aborted\n");
  258.     exit(1);
  259.   }
  260. }
  261.